package com.lh0811.forge.pserv.uacs.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_code.BeanHelper;
import com.lh0811.forge.dependency.basic_code.IdWorker;
import com.lh0811.forge.dependency.basic_web.exception.CommonException;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.SecurityUtil;
import com.lh0811.forge.dependency.security.core.model.entity.SecurityAuthority;
import com.lh0811.forge.pserv.basic.api.api.BizNoBaseApi;
import com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope;
import com.lh0811.forge.pserv.uacs.api.constant.UacsContant;
import com.lh0811.forge.pserv.uacs.api.param.UacsPlateTenantInfoCreateParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsPlateTenantInfoQueryListParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsPlateTenantInfoQueryPageParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsPlateTenantInfoUpdateParam;
import com.lh0811.forge.pserv.uacs.api.vo.UacsPlateTenantInfoQueryVo;
import com.lh0811.forge.pserv.uacs.server.repository.dao.UacsPlateTenantInfoDao;
import com.lh0811.forge.pserv.uacs.server.repository.dao.UacsTenantDepartmentDao;
import com.lh0811.forge.pserv.uacs.server.repository.entity.UacsPlateTenantInfo;
import com.lh0811.forge.pserv.uacs.server.repository.entity.UacsTenantDepartment;
import com.lh0811.forge.pserv.uacs.server.service.PlateTenantService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class PlateTenantServiceImpl implements PlateTenantService {

    @Resource
    private UacsPlateTenantInfoDao uacsPlateTenantInfoDao;

    @Resource
    private BizNoBaseApi bizNoBaseApi;

    @Resource
    private UacsTenantDepartmentDao uacsTenantDepartmentDao;

    @Override
    public void plateTenantCreate(UacsPlateTenantInfoCreateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        if (uacsPlateTenantInfoDao.lambdaQuery().eq(UacsPlateTenantInfo::getTenantName, param.getTenantName()).exists()) {
            throw CommonException.create(ServerResponse.createByError("租户名称已经存在"));
        }
        UacsPlateTenantInfo uacsPlateTenantInfo = new UacsPlateTenantInfo();
        uacsPlateTenantInfo.setId(IdWorker.nextId());
        uacsPlateTenantInfo.setTenantNo(bizNoBaseApi.getNextNumByBizType(UacsContant.BizNoType.平台租户编号.getBizType()));
        uacsPlateTenantInfo.setTenantName(param.getTenantName());
        uacsPlateTenantInfo.setContactsName(param.getContactsName());
        uacsPlateTenantInfo.setContactsEmail(param.getContactsEmail());
        uacsPlateTenantInfo.setContactsPhone(param.getContactsPhone());
        uacsPlateTenantInfo.setActiveFlag(param.getActiveFlag());
        uacsPlateTenantInfo.setCreateTime(new Date());
        uacsPlateTenantInfo.setCreateBy(Long.valueOf(securityAuthority.getUserId()));
        uacsPlateTenantInfoDao.save(uacsPlateTenantInfo);
    }

    @Override
    public void plateTenantDelete(Long tenantId) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        UacsPlateTenantInfo uacsPlateTenantInfo = uacsPlateTenantInfoDao.getById(tenantId);
        if (ObjectUtils.isEmpty(uacsPlateTenantInfo)) {
            throw CommonException.create(ServerResponse.createByError("租户信息不存在"));
        }
        if (uacsTenantDepartmentDao.lambdaQuery().eq(UacsTenantDepartment::getTenantId, tenantId).exists()) {
            throw CommonException.create(ServerResponse.createByError("租户下有组织机构信息不可删除"));
        }
        uacsPlateTenantInfoDao.removeById(tenantId);
    }

    @Override
    public void plateTenantUpdate(UacsPlateTenantInfoUpdateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        UacsPlateTenantInfo uacsPlateTenantInfo = uacsPlateTenantInfoDao.getById(param.getId());
        if (ObjectUtils.isEmpty(uacsPlateTenantInfo)) {
            throw CommonException.create(ServerResponse.createByError("租户信息不存在"));
        }

        if (StringUtils.isNotBlank(param.getTenantName()) && !StringUtils.equals(param.getTenantName(), uacsPlateTenantInfo.getTenantName())) {
            if (uacsPlateTenantInfoDao.lambdaQuery().eq(UacsPlateTenantInfo::getTenantName, param.getTenantName()).exists()) {
                throw CommonException.create(ServerResponse.createByError("租户名称已存在"));
            }
        }

        UacsPlateTenantInfo updateInfo = BeanHelper.copyBean(param, UacsPlateTenantInfo.class);
        updateInfo.setUpdateTime(new Date());
        updateInfo.setUpdateBy(Long.valueOf(securityAuthority.getUserId()));
        uacsPlateTenantInfoDao.updateById(updateInfo);
    }

    @Override
    @DataScope
    public PageInfo<UacsPlateTenantInfoQueryVo> plateTenantQueryPage(UacsPlateTenantInfoQueryPageParam param) throws Exception {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<UacsPlateTenantInfo> list = uacsPlateTenantInfoDao.lambdaQuery()
                .in(!CollectionUtils.isEmpty(param.getIdList()), UacsPlateTenantInfo::getId, param.getIdList())
                .like(StringUtils.isNotBlank(param.getTenantName()), UacsPlateTenantInfo::getTenantName, param.getTenantName())
                .like(StringUtils.isNotBlank(param.getContactsName()), UacsPlateTenantInfo::getContactsName, param.getContactsName())
                .like(StringUtils.isNotBlank(param.getContactsPhone()), UacsPlateTenantInfo::getContactsPhone, param.getContactsPhone())
                .eq(ObjectUtils.isNotEmpty(param.getActiveFlag()), UacsPlateTenantInfo::getActiveFlag, param.getActiveFlag())
                .orderByDesc(UacsPlateTenantInfo::getCreateTime)
                .list();
        PageInfo uacsPlateTenantInfoPageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        uacsPlateTenantInfoPageInfo.setList(BeanHelper.copyList(list, UacsPlateTenantInfoQueryVo.class));
        return uacsPlateTenantInfoPageInfo;
    }

    @Override
    @DataScope
    public List<UacsPlateTenantInfoQueryVo> plateTenantQueryList(UacsPlateTenantInfoQueryListParam param) throws Exception {
        List<UacsPlateTenantInfo> uacsPlateTenantInfoList = uacsPlateTenantInfoDao.lambdaQuery()
                .in(!CollectionUtils.isEmpty(param.getIdList()), UacsPlateTenantInfo::getId, param.getIdList())
                .like(StringUtils.isNotBlank(param.getTenantName()), UacsPlateTenantInfo::getTenantName, param.getTenantName())
                .like(StringUtils.isNotBlank(param.getContactsName()), UacsPlateTenantInfo::getContactsName, param.getContactsName())
                .like(StringUtils.isNotBlank(param.getContactsPhone()), UacsPlateTenantInfo::getContactsPhone, param.getContactsPhone())
                .eq(ObjectUtils.isNotEmpty(param.getActiveFlag()), UacsPlateTenantInfo::getActiveFlag, param.getActiveFlag())
                .orderByDesc(UacsPlateTenantInfo::getCreateTime)
                .list();
        return BeanHelper.copyList(uacsPlateTenantInfoList, UacsPlateTenantInfoQueryVo.class);
    }


}
